<?php

namespace app\bis\controller;
use think\Controller;

class Location extends  Base
{
    /**
     * @return mixed列表页
     */
    public function index()
    {
        $bisId = $this->getLoginUser()->bis_id;
        $locations=model('BisLocation')->getNormalLocationByBisIdStatus($bisId,['>',-1]);
        return $this->fetch('',['locations'=>$locations]);
    }

    public function add()
    {
        if (request()->isPost()) {
            // 第一点 检验数据 tp5 validate机制， 小伙伴自行完成

            $data = input('post.');
            $validate = validate('BisLocation');
            if(!$validate->scene('branchadd')->check($data)) {
                $this->error($validate->getError());
            }

            $bisId = $this->getLoginUser()->bis_id;
            $data['cat'] = '';
            if (!empty($data['se_category_id'])) {
                $data['cat'] = implode('|', $data['se_category_id']);
            }

            // 获取经纬度
            $lnglat = \Map::getLngLat($data['address']);
            if (empty($lnglat) || $lnglat['status'] != 0 || $lnglat['result']['precise'] != 1) {
                $this->error('无法获取数据，或者匹配的地址不精确');
            }

            // 门店入库操作
            // 总店相关信息入库
            $locationData = [
                'bis_id' => $bisId,
                'name' => $data['name'],
                'logo' => $data['logo'],
                'tel' => $data['tel'],
                'contact' => $data['contact'],
                'category_id' => $data['category_id'],
                'category_path' => $data['category_id'] . ',' . $data['cat'],
                'city_id' => $data['city_id'],
                'city_path' => empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'] . ',' . $data['se_city_id'],
                'address' => $data['address'],
                'api_address' => $data['address'],
                'open_time' => $data['open_time'],
                'content' => empty($data['content']) ? '' : $data['content'],
                'is_main' => 0,
                'xpoint' => empty($lnglat['result']['location']['lng']) ? '' : $lnglat['result']['location']['lng'],
                'ypoint' => empty($lnglat['result']['location']['lat']) ? '' : $lnglat['result']['location']['lat'],
                'status' => 0,
            ];
            if (!empty($data['id'])){
                return $this->update($locationData,$data['id']);
            }
            $locationId = model('BisLocation')->add($locationData);
            if ($locationId) {
                $this->success('门店申请成功', url('location/index'));
            } else {
                $this->error('门店申请失败');
            }
        } else {
            //获取一级城市的数据
            $citys = model('City')->getNormalCitysByParentId();
            //获取一级栏目的数据
            $categorys = model('Category')->getNormalCategoryByParentId();
            return $this->fetch('', [
                'citys' => $citys,
                'categorys' => $categorys,
            ]);
        }
    }

    public function edit($id)
    {
        $location=model('BisLocation')->get($id);
        //获取一级城市的数据
        $citys = model('City')->getNormalCitysByParentId();
        //获取一级栏目的数据
        $categorys = model('Category')->getNormalCategoryByParentId();
        $secategorys = model('Category')->getNormalCategoryByParentId($location['category_id']);
        $catchecked=getCategoryByPath($location['category_path']);
        return $this->fetch('',[
            'location'=>$location,
            'citys' => $citys,
            'categorys' => $categorys,
            'secategorys' => $secategorys,
            'catchecked' => $catchecked]);
    }

    public function update($data,$id)
    {

        $res=model('BisLocation')->save($data,['id' =>intval($id)]);
        if($res){
            $this->success('更新成功', url('location/index'));
        }else{
            $this->error('更新失败');
        }
    }

    public function delist()
    {
        $bisId = $this->getLoginUser()->bis_id;
        $locations=model('BisLocation')->getNormalLocationByBisIdStatus($bisId,-1);
        return $this->fetch('',['locations'=>$locations]);
    }
}